from django.db import models
from django.utils.translation import ugettext_lazy as _
from djiape.products.models import Product
from django.contrib.auth.models import User
from django.contrib import admin
from djiape.accounting.models import Account as GlAccount, Journal_type

class Location(models.Model):
    id = models.CharField(_('id'), max_length=4, primary_key=True)
    name = models.CharField(_('name'), max_length=30)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["name"]
        verbose_name = _('location')
        verbose_name_plural = _('locations')
    
admin.site.register(Location)
    

class Movement_Type(models.Model):
    MOVEMENT_CHOICES = (
        ('E', _('entry')),
        ('S', _('exit')),
        ('C', _('count')),
        ('T', _('transfer')),
    )
    name = models.CharField(_('name'), max_length=30)
    code = models.CharField(_('abbreviature'), max_length=2, unique=True)
    type = models.CharField(_('type'), max_length=1, choices=MOVEMENT_CHOICES)
    consecutive = models.CharField(_('consecutive'), max_length=10, blank=True, null=True)
    journal_type = models.ForeignKey(Journal_type, verbose_name=_('journal type'), blank=True, null=True)
    journal = models.CharField(_('journal'), max_length=10, blank=True, null=True)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["id"]
        verbose_name = _('movement type')
        verbose_name_plural = _('movement types')
    
admin.site.register(Movement_Type)

class Inventory(models.Model):
    location = models.ForeignKey(Location, verbose_name=_('location'))
    product = models.ForeignKey(Product, verbose_name=_('product'))
    position = models.CharField(_('position'), max_length=10)
    stock = models.DecimalField(_('stock'), max_digits=14, decimal_places=2, default=0)
    assign = models.DecimalField(_('assign'), max_digits=14, decimal_places=2, default=0)
    ordered = models.DecimalField(_('ordered'), max_digits=14, decimal_places=2, default=0)
    maximum = models.DecimalField(_('maximum'), max_digits=14, decimal_places=2, default=0)
    minimum = models.DecimalField(_('minimum'), max_digits=14, decimal_places=2, default=0)
    reorder = models.DecimalField(_('reorder'), max_digits=14, decimal_places=2, default=0)

class Movement(models.Model):
    type = models.ForeignKey(Movement_Type, verbose_name=_('movement type'))
    reference = models.CharField(_('reference'), max_length=30)
    date = models.DateField(_('date'), auto_now_add=True)
    location1 = models.ForeignKey(Location, verbose_name=_('location 1'), related_name='location1')
    location2 = models.ForeignKey(Location, verbose_name=_('location 2'), related_name='location2', blank=True, null=True)
    quantity = models.DecimalField(_('quantity'), max_digits=14, decimal_places=2)
    Product = models.ForeignKey(Product, verbose_name=_('product'))
    cost = models.DecimalField(_('quantity'), max_digits=14, decimal_places=2)
    user = models.ForeignKey(User, verbose_name = _('user'))
    status = models.CharField(_('status'), max_length=1, default='N')
    gl_operation = models.IntegerField(_('gl operation'), default=0)
    
